package com.rxh.service.impl;

import com.rxh.mapper.finance.FinanceBusinessMoneyMapper;
import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.pojo.finance.FinanceBusinessMoney;
import com.rxh.service.BusinessMoneyService;
import com.rxh.vo.VoFinanceBusinessMoney;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author xuzm
 * @date 20180404
 *
 */
@Service
public class BusinessMoneyServiceImpl implements BusinessMoneyService
{

	@Autowired
	FinanceBusinessMoneyMapper businessMoneyMapper;
	
	@Autowired
    private MerchantInfoMapper merchantInfoMapper;
	
	/**
	 * 查询商户余额统计
	 * 
	 */
	@Override
	public FinanceBusinessMoney queryBusinessMoneyStatistics(Integer merId) {
		FinanceBusinessMoney businessMoneyStatistics = new FinanceBusinessMoney();
		Map<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("merId",merId);
		VoFinanceBusinessMoney financeBusinessMoney = new VoFinanceBusinessMoney();
		financeBusinessMoney.setRefId(merId);
		//查询最近一次有数据的统计信息
		financeBusinessMoney = businessMoneyMapper.selectByParam(financeBusinessMoney);
		if(null != financeBusinessMoney){
			paramMap.put("createTime", financeBusinessMoney.getCreateTime());
		}
		
		//查询直到前天的余额统计
		FinanceBusinessMoney businessYesterdayMoney = businessMoneyMapper.queryBusinessMoney(paramMap);
		//查询商户今天进来的交易的余额统计
		FinanceBusinessMoney businessTodayMoney = businessMoneyMapper.queryTodayBusinessMoney(paramMap);
		
		if(null != businessYesterdayMoney){
			
			businessMoneyStatistics.setAmount(businessYesterdayMoney.getAmount().add(businessTodayMoney.getAmount()));
			businessMoneyStatistics.setUsableAmount(businessYesterdayMoney.getUsableAmount().add(businessTodayMoney.getUsableAmount()));
			businessMoneyStatistics.setDisabledAmount(businessYesterdayMoney.getDisabledAmount().add(businessTodayMoney.getDisabledAmount()));
			businessMoneyStatistics.setAuditAmount(businessYesterdayMoney.getAuditAmount().add(businessTodayMoney.getAuditAmount()));
			businessMoneyStatistics.setSuccessAmount(businessYesterdayMoney.getSuccessAmount().add(businessTodayMoney.getSuccessAmount()));
			businessMoneyStatistics.setDeduct(businessYesterdayMoney.getDeduct().add(businessTodayMoney.getDeduct()));
			businessMoneyStatistics.setBond(businessYesterdayMoney.getBond().add(businessTodayMoney.getBond()));
			businessMoneyStatistics.setProtestAmount(businessYesterdayMoney.getProtestAmount().add(businessTodayMoney.getProtestAmount()));
			businessMoneyStatistics.setRefundAmount(businessYesterdayMoney.getRefundAmount().add(businessTodayMoney.getRefundAmount()));
			businessMoneyStatistics.setProtestChargeAmount(businessYesterdayMoney.getProtestChargeAmount().add(businessTodayMoney.getProtestChargeAmount()));
			businessMoneyStatistics.setReturnDeductAmount(businessYesterdayMoney.getReturnDeductAmount().add(businessTodayMoney.getReturnDeductAmount()));
			businessMoneyStatistics.setBookBalance(businessYesterdayMoney.getBookBalance().add(businessTodayMoney.getBookBalance()));
				
		}
		
		
		if(null == businessYesterdayMoney){
			
			
			return businessTodayMoney;
			
			
		}
		
		return businessMoneyStatistics;
		
		
	}

	@Override
	public Map<String, Object> getBusinessMoneyStatistics(Integer merId) {
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		Map<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("merId",merId);
			
		FinanceBusinessMoney businessYesterdayMoney = businessMoneyMapper.queryBusinessMoney(paramMap);
		VoFinanceBusinessMoney settlePlusAmount = businessMoneyMapper.querySettlePlusAmount(paramMap);
		VoFinanceBusinessMoney nosettlePlusAmount = businessMoneyMapper.queryNosettlePlusAmount(paramMap);
		VoFinanceBusinessMoney deductPlusAmount = businessMoneyMapper.queryDeductPlusAmount(paramMap);
		VoFinanceBusinessMoney bondReleaseAmount = businessMoneyMapper.queryBondReleaseAmount(paramMap);
		VoFinanceBusinessMoney bondNoReleaseAmount = businessMoneyMapper.queryBondNoReleaseAmount(paramMap);
		VoFinanceBusinessMoney refundPlusAmount = businessMoneyMapper.queryRefundPlusAmount(paramMap);
		VoFinanceBusinessMoney protestAndFeePlusAmount = businessMoneyMapper.queryProtestAndFeePlusAmount(paramMap);
		VoFinanceBusinessMoney drawingNoAuditPlusAmount = businessMoneyMapper.queryDrawingNoAuditPlusAmount(paramMap);
		VoFinanceBusinessMoney drawingAuditPlusAmount = businessMoneyMapper.queryDrawingAuditPlusAmount(paramMap);
		VoFinanceBusinessMoney returnDeductAmount = businessMoneyMapper.queryReturnDeductAmount(paramMap);
		
	    //可用余额合计
	    BigDecimal finalUsableAmount = new BigDecimal(0);

	    //未释放的保证金合计
	    BigDecimal finalBondAmount = new BigDecimal(0);
	    
	    //未清算金额合计
	    BigDecimal finalNoSettleAmount = new BigDecimal(0);
	    
		if(null != businessYesterdayMoney){
			
			BigDecimal preUsableAmount  =  (null == businessYesterdayMoney.getUsableAmount()? new BigDecimal(0):businessYesterdayMoney.getUsableAmount());
			resultMap.put("preUsableAmount", preUsableAmount);
			
			BigDecimal preBondAmount  = null == businessYesterdayMoney.getBond()? new BigDecimal(0):businessYesterdayMoney.getBond();
			resultMap.put("preBondAmount", preBondAmount);
			
			BigDecimal preDisabledAmount  = null == businessYesterdayMoney.getDisabledAmount()? new BigDecimal(0):businessYesterdayMoney.getDisabledAmount();
			resultMap.put("preDisabledAmount", preDisabledAmount);

			finalUsableAmount.add(preUsableAmount).add(preBondAmount);
			finalBondAmount.add(preBondAmount);
			finalNoSettleAmount.add(preDisabledAmount);
			
		}else if(null == businessYesterdayMoney){
			
			resultMap.put("preUsableAmount", 0);
			resultMap.put("preBondAmount", 0);
			resultMap.put("preDisabledAmount", 0);
			
		}
		
		if(null != settlePlusAmount){
			
			BigDecimal settlePlusAmount_2 = (null == settlePlusAmount.getSettlePlusAmount()? new BigDecimal(0):settlePlusAmount.getSettlePlusAmount());
			resultMap.put("settlePlusAmount",settlePlusAmount_2);
			
			finalUsableAmount.add(settlePlusAmount_2);
			finalNoSettleAmount.subtract(settlePlusAmount_2);
			
		}
		
		if(null != nosettlePlusAmount){
			
			BigDecimal nosettlePlusAmount_2 = (null == nosettlePlusAmount.getNosettlePlusAmount()? new BigDecimal(0):nosettlePlusAmount.getNosettlePlusAmount());

			resultMap.put("nosettlePlusAmount", nosettlePlusAmount_2);
		}
		
		
		if(null != deductPlusAmount){
			
			BigDecimal deductPlusAmount_2 = (null == deductPlusAmount.getDeductPlusAmount()? new BigDecimal(0):deductPlusAmount.getDeductPlusAmount());
			resultMap.put("deductPlusAmount",deductPlusAmount_2);
			
			finalUsableAmount.subtract(deductPlusAmount_2);
			
		}
		
		if(null != bondReleaseAmount){
			
			BigDecimal bondReleaseAmount_2 = (null == bondReleaseAmount.getBondNoReleaseAmount()? new BigDecimal(0):bondReleaseAmount.getBondNoReleaseAmount());
			resultMap.put("bondReleaseAmount",bondReleaseAmount_2);
			finalBondAmount.subtract(bondReleaseAmount_2);
			
		}
		
		if(null != bondNoReleaseAmount){
			
			BigDecimal bondNoReleaseAmount_2 = ( null == bondNoReleaseAmount.getSettlePlusAmount()? new BigDecimal(0):bondNoReleaseAmount.getSettlePlusAmount());
			resultMap.put("bondNoReleaseAmount",bondNoReleaseAmount_2);
			finalBondAmount.add(bondNoReleaseAmount_2);
		}
		
		if(null != refundPlusAmount){
			
			BigDecimal refundPlusAmount_2 = ( null == refundPlusAmount.getRefundPlusAmount()? new BigDecimal(0):refundPlusAmount.getRefundPlusAmount());
			resultMap.put("refundPlusAmount",refundPlusAmount_2);
			
			finalUsableAmount.subtract(refundPlusAmount_2);
			
		}
		
		if(null != returnDeductAmount){
			
			BigDecimal returnDeductAmount_2 = ( null == returnDeductAmount.getReturnDeductAmount()? new BigDecimal(0):returnDeductAmount.getReturnDeductAmount());
			resultMap.put("returnDeductAmount",returnDeductAmount_2);
			
			finalUsableAmount.add(returnDeductAmount_2);
			
		}
		
		if(null != protestAndFeePlusAmount){
			
			BigDecimal protestPlusAmount_2 = (null == protestAndFeePlusAmount.getProtestPlusAmount()? new BigDecimal(0):protestAndFeePlusAmount.getProtestPlusAmount());
			resultMap.put("protestPlusAmount", protestPlusAmount_2);
			
			BigDecimal feePlusAmount_2 = (null == protestAndFeePlusAmount.getProtestChargePlusAmount()? new BigDecimal(0):protestAndFeePlusAmount.getProtestChargePlusAmount());
			resultMap.put("feePlusAmount",feePlusAmount_2 );

			finalUsableAmount.subtract(protestPlusAmount_2).subtract(feePlusAmount_2);
			
		}
		
		if(null != drawingNoAuditPlusAmount){
			
			BigDecimal drawingNoAuditPlusAmount_2 = (null == drawingNoAuditPlusAmount.getDrawingNoAuditPlusAmount()? new BigDecimal(0):drawingNoAuditPlusAmount.getDrawingNoAuditPlusAmount());
			resultMap.put("drawingNoAuditPlusAmount",drawingNoAuditPlusAmount_2);
			
			finalUsableAmount.subtract(drawingNoAuditPlusAmount_2);
			
		}
		
		if(null != drawingAuditPlusAmount){
			
			BigDecimal drawingAuditPlusAmount_2 = (null == drawingAuditPlusAmount.getDrawingAuditPlusAmount()? new BigDecimal(0):drawingAuditPlusAmount.getDrawingAuditPlusAmount());
			resultMap.put("drawingAuditPlusAmount",drawingAuditPlusAmount_2);
			
			finalUsableAmount.subtract(drawingAuditPlusAmount_2);
			
		}
		
		resultMap.put("finalUsableAmount",finalUsableAmount);
		resultMap.put("finalBondAmount",finalBondAmount);
		resultMap.put("finalNoSettleAmount",finalNoSettleAmount);
		
		return resultMap;
				
	}

	@Override
	public Map<String, Object> getMerIdsInit() {
		
		  Map<String, Object> resultMap = new HashMap<>();
		  resultMap.put("merchants", merchantInfoMapper.selectAllIdAndName());
		  
		  return resultMap;
		  
	}

	@Override
	public FinanceBusinessMoney getMerchantBalance(Integer merchantId) {
		return businessMoneyMapper.selectByMerchantId(merchantId);
	}
}
